import type { Map, MapMouseEvent, MapWheelEvent } from 'mapbox-gl'
import { unSelectMapItem, selectMapItemOnMap } from '@/map-item'
import type { Interaction } from './types'

export function isRight(evt: MapMouseEvent): boolean {
  return evt.originalEvent.button == 2
}

export class NormalInteraction implements Interaction {
  public readonly type: string = 'Normal'
  private cx = 0
  private cy = 0

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public attach(map: Map): void {
    this.cx = 0
    this.cy = 0
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public detach(map: Map): void {}

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onMouseEnter(evt: MapMouseEvent): void {}
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onMouseLeave(evt: MapMouseEvent): void {}
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onMouseMove(evt: MapMouseEvent): void {}
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onMouseUp(evt: MapMouseEvent): void {
    const me = evt.originalEvent
    const dx = this.cx - me.clientX
    const dy = this.cy - me.clientY
    const eps = 10
    const drag = dx < -eps || dx > eps || dy < -eps || dy > eps
    if (drag) {
      return
    }
    const right = isRight(evt)
    if (right) {
      unSelectMapItem()
      return
    }
    selectMapItemOnMap(evt)
  }
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onMouseDown(evt: MapMouseEvent): void {
    const me = evt.originalEvent
    this.cx = me.clientX
    this.cy = me.clientY
  }
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onWheel(evt: MapWheelEvent): void {}
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  public onKeyDown(evt: KeyboardEvent): void {}
}
